<?php
/**
 * 博主相关控制器继承基类
 *
 */
namespace haibao\cms\business\control\pc\channel\blog;

use haibao\cms\business\BusinessException;
use haibao\cms\data\control\BlogController as BlogControllerData;

class BlogController extends \haibao\cms\business\control\pc\channel\ChannelController{
	
	private static $_instance;
	private $blogData;
	public function __construct($updateTime='',$controlId='',$isPublish=''){
		$this->blogData = new BlogControllerData;
		parent::__construct($updateTime,$controlId,$isPublish);
	}
	public function __clone(){
	}
	
	public static function getBlogInstance(){
		if(!(self::$_instance instanceof self)){
			self::$_instance = new self;
			self::$_instance->blogData = new BlogControllerData;
		}
		return self::$_instance;
	}
	
	public function getUserListByCateId($cateId){
		if(!$cateId){
			throw new BusinessException('分类不能为空',\haibao\cms\business\Base::DATA_EMPTY);
		}
		$userList = $this->blogData->getUserListByCateId($cateId);
		return $userList;
	}
	
	public function getBlogListByUserIds($userIds,$limit=10,$orderBy=BlogControllerData::ORDER_BY_DATE,$page=1){
		if(!$userIds){
			throw new BusinessException('用户数据不能为空',\haibao\cms\business\Base::DATA_EMPTY);
		}
		$blogList = $this->blogData->getBlogListByUserId($userIds,$limit,$orderBy,$page);
		return $blogList;
	}
	
	public function getBlogIntroPic($blogId){
		if(!$blogId){
			throw new BusinessException('博主ID不能为空',\haibao\cms\business\Base::DATA_EMPTY);
		}
		$blogIntroPic = $this->blogData->getBlogIntroPic($blogId);
		return $blogIntroPic;
	}
	
	public function getBlogInfoByBlogId($blogId,$isGetMessage=true){
		if(!$blogId){
			throw new BusinessException('博客ID不能为空',\haibao\cms\business\Base::DATA_EMPTY);
		}
		$blogInfo = $this->blogData->getBlogInfoByBlogId($blogId,$isGetMessage);
		return $blogInfo;
	}
	
	public function getBloggerInfoById($bloggerId,$isGetAll = true){
		if(!$bloggerId){
			throw new BusinessException('博主ID不能为空',\haibao\cms\business\Base::DATA_EMPTY);
		}
		$bloggerInfo = $this->blogData->getBloggerInfoById($bloggerId,$isGetAll);
		if($bloggerInfo && $isGetAll){
			$bloggerInfo['bloggerProfile'] = \haibao\cms\business\control\PublicController::perfectStr($bloggerInfo['bloggerProfile'],80);
			$bloggerInfo['bloggerUrl'] = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_USER).'/space/'.$bloggerId.'/';
			$bloggerInfo['bloggerImg'] = self::getBloggerAvatar($bloggerId,"150_150");
			$bloggerInfo['bloggerCate'] = '';
			$cateInfo = self::getCateInfo($bloggerInfo['cate_id'],$bloggerId);
			if($cateInfo){
				$bloggerInfo['bloggerCate'] = $cateInfo['name'];
			}
		}
		return $bloggerInfo;
	}
	
	/**
	 * 按照时间排序获取博客列表
	 * @cateId   分类id
	 * @page     页码
	 */
	public function getBlogListByCateId($cateId,$page=1,$orderBy = \haibao\cms\data\control\BlogController::ORDER_BY_DATE){
		if(!$cateId){
			throw new BusinessException('分类ID不能为空',\haibao\cms\business\Base::DATA_EMPTY);
		}
		return $this->blogData->getBlogListByCateId($cateId,$page,$orderBy);
	}
	
	/**
	 * 获取频道或者用户底下的博客数
	 * @param  $objectId
	 * @param  $type    类型(频道/用户)
	 */
	public function getResultCountByObjectId($objectId,$type){
		if(!$objectId){
			throw new BusinessException('类目Id不能为空',\haibao\cms\business\Base::DATA_EMPTY);
		}
		return $this->blogData->getResultCountByObjectId($objectId,$type);
	}

	public function getBlogHotTags($blogId){
		if(!$blogId){
			throw new BusinessException('博主ID不能为空',\haibao\cms\business\Base::DATA_EMPTY);
		}
	
		return $this->blogData->getBlogHotTags($blogId);
	}
	
	public static function getBloggerAvatar ($uid,$size='42_42') {
		$sizevalue   = 'big';
		$dest_size   = explode("_", $size);
		$dest_width  = $dest_size[0];
		$dest_height = $dest_size[1];
	
		if ($dest_width < 70) {
			$sizevalue   = 'small';
		}else if ($dest_width < 100) {
			$sizevalue   = 'middle';
		}
		$avatarurl = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_BBS)."/uc_server/avatar.php?uid={$uid}&size={$sizevalue}";
		return $avatarurl;
	}
	
	public static function getCateInfo($keyId,$isSearch=false,$searchType='tagId'){
		$cateInfo = array(
			'91'=>array('name'=>'自媒体精英','featureClassId'=>\haibao\cms\model\data\FeaturePublish::CLASS_FASHION,'tagId'=>11175),
			'92'=>array('name'=>'明星名模','featureClassId'=>\haibao\cms\model\data\FeaturePublish::CLASS_STAR,'tagId'=>11176),
			'93'=>array('name'=>'时装博主','featureClassId'=>\haibao\cms\model\data\FeaturePublish::CLASS_FASHION,'tagId'=>11177),
			'94'=>array('name'=>'美容博主','featureClassId'=>\haibao\cms\model\data\FeaturePublish::CLASS_PAINT,'tagId'=>11178),
			'95'=>array('name'=>'视觉展','featureClassId'=>\haibao\cms\model\data\FeaturePublish::CLASS_FASHION,'tagId'=>11179),
			'96'=>array('name'=>'摄影师','featureClassId'=>\haibao\cms\model\data\FeaturePublish::CLASS_FASHION,'tagId'=>0),
			'98'=>array('name'=>'海报明星编辑团','featureClassId'=>\haibao\cms\model\data\FeaturePublish::CLASS_FASHION,'tagId'=>0),
			'99'=>array('name'=>'明星','featureClassId'=>\haibao\cms\model\data\FeaturePublish::CLASS_FASHION,'tagId'=>0),
			'100'=>array('name'=>'名模','featureClassId'=>\haibao\cms\model\data\FeaturePublish::CLASS_FASHION,'tagId'=>0),
			'101'=>array('name'=>'化妆师','featureClassId'=>\haibao\cms\model\data\FeaturePublish::CLASS_FASHION,'tagId'=>0),
			'102'=>array('name'=>'造型师','featureClassId'=>\haibao\cms\model\data\FeaturePublish::CLASS_FASHION,'tagId'=>0),
			'103'=>array('name'=>'设计师','featureClassId'=>\haibao\cms\model\data\FeaturePublish::CLASS_FASHION,'tagId'=>0)
		);
		if($keyId == 0){
			return $cateInfo;
		}
		if($isSearch){
			foreach($cateInfo as $key=>$info){
				if($info[$searchType] == $keyId){
					return array($cateInfo[$key],$key);
					break;
				}
			}
		}
		if(isset($cateInfo[$keyId])){
			return $cateInfo[$keyId];
		}
		return array(
				'name'=>'海报明星',
				'featureClassId'=>\haibao\cms\model\data\FeaturePublish::CLASS_FASHION,
				'tagId'=>11175);
	}
	
	/**
	 * 博主文章添加点击数
	 */
	public static function addBlogClickNum($blogId){
		if(!$blogId){
			throw new BusinessException('博主ID不能为空',\haibao\cms\business\Base::DATA_EMPTY);
		}
		$data = new BlogControllerData;
		$data->addBlogClickNum((int)$blogId);
	}
	
	/**
	 * 博主更新评论数
	 */
	public static function updateCommentNum($blogId,$num=1,$isSub=false){
		if(!$blogId){
			throw new BusinessException('博主ID不能为空',\haibao\cms\business\Base::DATA_EMPTY);
		}
		$data = new BlogControllerData;
		$data->updateCommentNum((int)$blogId,$num,$isSub);
	}
}

